package com.orangelife.utils;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import cyan.svc.horoshu.http.HttpReqChain;
import cyan.svc.horoshu.http.HttpRequest;
import cyan.svc.horoshu.http.HttpResp;
import cyan.svc.horoshu.http.HttpSvc;
import cyan.svc.horoshu.http.IHttpRespHandler;

/****
 * https://github.com/DreamInSun/Horoshu/blob/master/Horoshu-Core/src/test/java/
 * cyan/svc/horoshu/http/HttpSvcTest.java
 * 
 * @author shendiao
 * 
 */
public class HttpSvcUtils {
	HttpSvc.IHook hooks = new HttpSvc.IHook() {

		@Override
		public void postDns(HttpReqChain arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void postInvoke(HttpReqChain arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void preInvoke(HttpReqChain arg0) {
			// TODO Auto-generated method stub

		}
	};

	/* ===== Create Response Handler ===== */
	IHttpRespHandler respHandler = new IHttpRespHandler() {
		@Override
		public void procHttpResponse(long reqId, HttpResp resp) {

			resp.print();
		}
	};

	/***
	 * svc get方法提交 返回 阿里巴巴 JSONObject
	 * 
	 * @param httpUrl
	 *            请求地址
	 * @param accessToken
	 *            授权 （非必填）
	 * @return
	 */
	public static JSONObject doGetSvc(String httpUrl, String accessToken) {

		/***
		 * com.google.gson.JsonObject;
		 */
		// JsonObject gObj = HttpSvc.build(httpUrl).get().getJsonObject();
		/***
		 * com.alibaba.fastjson.JSONObject;
		 */
		JSONObject jsonObject = null;
		/**
		 * token为非必填的情况
		 */

		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl)
					.setHeader("accessToken", accessToken).get()
					.getJSONObject();
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl).get().getJSONObject();
		}

		return jsonObject;
	}

	/***
	 * 
	 * @param httpUrl
	 *            请求地址
	 * @param parMap
	 *            参数
	 * @param accessToken
	 *            授权 （非必填）
	 * @return
	 */
	public static JSONObject doGetSvc(String httpUrl,
			Map<String, Object> parMap, String accessToken) {
		JSONObject jsonObject = null;
		if (parMap == null) {
			return jsonObject;
		}
		/***
		 * com.google.gson.JsonObject;
		 */
		// JsonObject gObj = HttpSvc.build(httpUrl).get().getJsonObject();
		/***
		 * com.alibaba.fastjson.JSONObject;
		 */

		/**
		 * token为非必填的情况
		 */

		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl).setParams(parMap)
					.setHeader("accessToken", accessToken).get()
					.getJSONObject();
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl).setParams(parMap).get()
					.getJSONObject();
		}

		return jsonObject;
	}

	/***
	 * svc 通过提交map提交参数 post方法提交 返回 阿里巴巴 JSONObject json 格式提交
	 * 
	 * @param httpUrl
	 * @param paramMap
	 *            （提交参数 Map<String, Object>）
	 * @param accessToken
	 *            （非必填）
	 * @return
	 */
	public static JSONObject doPostInfoSvc(String httpUrl, Object object,
			String accessToken) {
		JSONObject jsonObject = null;

		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader("Content-Type", "application/json")
					.setContent(object, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("accessToken", accessToken)
					.setHeader("Content-Type", "application/json")
					.setContent(object, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();

		}

		return jsonObject;
	}

	public static JSONObject doPostSvc(String httpUrl,
			Map<String, Object> paramMap, String accessToken) {
		JSONObject jsonObject = null;
		if (paramMap == null) {
			return jsonObject;
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("Content-Type", "application/json")
					// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN,
					// "HKLJHJWEQPOWJ")
					// .setFragment("Chapter1").setHeaders(headerMap)
					.setContent(paramMap, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("accessToken", accessToken)
					.setHeader("Content-Type", "application/json")
					.setContent(paramMap, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();
			// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN,
			// "HKLJHJWEQPOWJ")
			// .setFragment("Chapter1").setHeaders(headerMap)
			// .setParams(paramMap).post().getJSONObject();

		}
		// .setContent(content, HttpRequest.CONTENT_TYPE_JSON)
		// .setRespHandler(respHandler).post().getJSONObject();
		return jsonObject;
	}

	/***
	 * 通过json 提交 字符串 提交post 返回 阿里巴巴 JSONObject json 格式提交
	 * 
	 * @param httpUrl
	 * @param json
	 *            必填
	 * @param accessToken
	 *            (非必填)
	 * @return
	 */

	public static JSONObject doPostSvc(String httpUrl, String json,
			String accessToken) {

		JSONObject jsonObject = null;
		if (StringUtils.isBlank(json)) {
			return jsonObject;
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("Content-Type", "application/json")
					// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN,
					// "HKLJHJWEQPOWJ")
					// .setFragment("Chapter1").setHeaders(headerMap)
					.setContent(json, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("accessToken", accessToken)
					.setHeader("Content-Type", "application/json")
					.setContent(json, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();
			// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN,
			// "HKLJHJWEQPOWJ")
			// .setFragment("Chapter1").setHeaders(headerMap)
			// .setParams(paramMap).post().getJSONObject();

		}
		// .setContent(content, HttpRequest.CONTENT_TYPE_JSON)
		// .setRespHandler(respHandler).post().getJSONObject();
		return jsonObject;
	}

	public static JSONObject doPostSvc(String httpUrl, List<String> list,
			String accessToken) {

		JSONObject jsonObject = null;
		if (list == null) {
			return jsonObject;
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("Content-Type", "application/json")
					// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN,
					// "HKLJHJWEQPOWJ")
					// .setFragment("Chapter1").setHeaders(headerMap)
					.setContent(list, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc
					.build()
					.setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_CONNECTION,
							HttpSvc.CONN_STAT_KEEP_ALIVE)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("accessToken", accessToken)
					.setHeader("Content-Type", "application/json")
					.setContent(list, HttpRequest.CONTENT_TYPE_JSON).post()
					.getJSONObject();
			// .setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN,
			// "HKLJHJWEQPOWJ")
			// .setFragment("Chapter1").setHeaders(headerMap)
			// .setParams(paramMap).post().getJSONObject();

		}
		// .setContent(content, HttpRequest.CONTENT_TYPE_JSON)
		// .setRespHandler(respHandler).post().getJSONObject();
		return jsonObject;
	}

	/***
	 * svc put方法提交 返回 阿里巴巴 JSONObject json 格式提交
	 * 
	 * @param httpUrl
	 * @param paramMap
	 *            必填
	 * @param accessToken
	 * @return
	 */

	public static JSONObject doPutSvc(String httpUrl,
			Map<String, Object> paramMap, String accessToken) {
		JSONObject jsonObject = null;
		if (paramMap == null) {
			return jsonObject;
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build().setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader("Content-Type", "application/json")
					.setContent(paramMap, HttpRequest.CONTENT_TYPE_JSON).put()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build().setURI(httpUrl)
					.setContent(paramMap, HttpRequest.CONTENT_TYPE_JSON)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader("accessToken", accessToken)
					.setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("Content-Type", "application/json").put()
					.getJSONObject();
		}
		// .setContent(content, HttpRequest.CONTENT_TYPE_PLAIN)
		// .setRespHandler(respHandler).put().getJSONObject();
		return jsonObject;

	}

	/***
	 * 通过 json 字符提交 put 方法
	 * 
	 * @param httpUrl
	 * @param json
	 *            （json 字符串） 必填
	 * @param accessToken
	 *            (非必填)
	 * @return
	 */
	public static JSONObject doPutSvc(String httpUrl, String json,
			String accessToken) {
		JSONObject jsonObject = null;
		if (StringUtils.isBlank(json)) {
			return jsonObject;
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build().setURI(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader("Content-Type", "application/json")
					.setContent(json, HttpRequest.CONTENT_TYPE_JSON).put()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build().setURI(httpUrl)
					.setContent(json, HttpRequest.CONTENT_TYPE_JSON)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader("accessToken", accessToken)
					.setHeader(HttpSvc.HEADER_FIELD_ACCESSTOKEN, accessToken)
					.setHeader("Content-Type", "application/json").put()
					.getJSONObject();
		}
		// .setContent(content, HttpRequest.CONTENT_TYPE_PLAIN)
		// .setRespHandler(respHandler).put().getJSONObject();
		return jsonObject;

	}

	/***
	 * svc delete 方法提交 返回 阿里巴巴 JSONObject json 格式提交
	 * 
	 * @param httpUrl
	 *            请求地址
	 * @param paramMap
	 *            参数(必)
	 * @param accessToken
	 *            授权 （非必填）
	 * @return
	 */
	public static JSONObject doDeletSvc(String httpUrl, String accessToken) {
		JSONObject jsonObject = null;
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0").delete()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader("accessToken", accessToken).delete()
					.getJSONObject();
		}
		return jsonObject;
	}

	/***
	 * svc delete 方法提交 返回 阿里巴巴 JSONObject json 格式提交
	 * 
	 * @param httpUrl
	 *            请求地址
	 * @param parMap
	 *            参数
	 * @param accessToken
	 *            授权 （非必填）
	 * @return
	 */
	public static JSONObject doDeletSvc(String httpUrl,
			Map<String, Object> parMap, String accessToken) {
		JSONObject jsonObject = null;
		if (parMap == null) {
			return jsonObject;
		}
		if (StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl).setParams(parMap)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0").delete()
					.getJSONObject();
		}
		if (!StringUtils.isBlank(accessToken)) {
			jsonObject = HttpSvc.build(httpUrl).setParams(parMap)
					.setHeader(HttpSvc.HEADER_FIELD_VERSION, "1.0.0")
					.setHeader("accessToken", accessToken).delete()
					.getJSONObject();
		}
		return jsonObject;
	}
}
